<?php

namespace App\Services;

use App\Models\Sys_User;
use App\Models\Sys_User_Store;
use Illuminate\Support\Facades\Hash;

class UserService
{
    public function getAppUserList($params)
    {
        $userids = Sys_User_Store::where('storeid',$params['storeid'])->pluck('userid');
        $result = Sys_User::whereIn('id',$userids)->Nodel()->get();
        $arr = [];
        foreach ($result as $key => $val){
            $arr[$key] = ['label' => $val->nickname,'value' => $val->id];
        }
        return $arr;
    }

    public function getUserList($params)
    {
        if(isset($params['uid'])){
            $storeids = Sys_User_Store::where('userid',$params['uid'])->pluck('storeid');
            $userids = Sys_User_Store::whereIn('storeid',$storeids)->pluck('userid');
            $result = Sys_User::getFilter($params)->whereIn('id',$userids)->Nodel()->get();
            $arr = [];
            foreach ($result as $key => $val){
                $arr[$key] = ['label' => $val->nickname,'value' => $val->id];
            }
            return $arr;
        }else{
            $result = Sys_User::getFilter($params)->Nodel()->get();
            $arr = [];
            foreach ($result as $key => $val){
                $arr[$key] = ['label' => $val->nickname,'value' => $val->id];
            }
            return $arr;
        }
        
    }

    public function getList($params)
    {
        $result = Sys_User::getFilter($params)->Nodel()->paginate($params['pageSize']);
        foreach ($result as $key => $value){
            $result[$key]['role_name'] = $value->hasOneRole->role_name;
            $result[$key]['is_super'] = $value->hasOneRole->is_super;
            $result[$key]['storeid'] = $value->hasManyStore()->pluck('storeid');
        }
        $result->data = $result->makeHidden(['hasOneRole']);
        $list['list'] = $result->items();
        $list['total'] = $result->total();
        return ['status' => true,'code' => 200,'data' => $list];
    }

    public function addUser($params)
    {
        $res = Sys_User::updateOrCreate(
            ['id' => $params['id']],
            [
                'username' => $params['username'],
                'mpassword' => $params['mpassword'],
                'password' => Hash::make($params['mpassword']),
                'roleid' => $params['roleid'],
                'status' => $params['status'],
                'nickname' => $params['nickname'],
                'realname' => $params['realname'],
                'phone' => $params['phone'],
            ],
        );
        if(isset($params['id']) && !empty($params['id'])){
            $userid = $params['id'];
            Sys_User_Store::where('userid',$params['id'])->delete();
        }else{
            $userid = $res->id;
        }
        foreach ($params['storeid'] as $value){
            Sys_User_Store::create([
                'userid' => $userid,
                'storeid' => $value
            ]);
        }
        if($res)
            return true;
        else
            return false;

    }

    public function delUser($params)
    {
        $res = Sys_User::where('id',$params['ids'])->update(['is_del' => 'Y']);
        if($res)
            return true;
        else
            return false;
    }
}
